package isjinhao.jlox.grammar.pop;

import isjinhao.jlox.grammar.expression.represention.Expression;
import isjinhao.jlox.grammar.statement.StatementParser;
import isjinhao.jlox.grammar.statement.represention.Return;
import isjinhao.jlox.grammar.statement.represention.Statement;
import isjinhao.jlox.lexical.TokenSequence;
import isjinhao.jlox.lexical.TokenType;

/**
 * Procedure Oriented Programming 语法使用到的 Statement语法
 * grammar:
 *  1、print            ->  "print" expression ";"
 *  2、varDefinition    ->  "var" (IDENTIFIER "=" expression)? ";"
 *  3、assignment       ->  IDENTIFIER "=" expression ";"
 *  4、return_value     ->  "return" expression ";"
 *  5、block            ->  "{" statement* "}"
 *  6、statement        ->  print | varDefinition | assignment | return_value | block
 *
 * @Author ISJINHAO
 * @Date 2021/2/28 9:23
 */
public class PopStatementParser extends StatementParser {

    public PopStatementParser(TokenSequence tokenSequence) {
        super(tokenSequence);
    }

    private Statement returnValue() {
        tokenSequence.advance();
        Expression expression = expressionParser.parse();
        return new Return(expression);
    }

    @Override
    protected Statement statement() {
        if(tokenSequence.curEqual(TokenType.RETURN)) {
            return returnValue();
        }
        return super.statement();
    }

}
